/**
 * Created by Wangwei on 2017/1/4.
 */
layui.define(['base'], function (exports) {
    var $setting=layui.config,$http=layui.http, $global = layui.common,
        data = {},auth='';data.fn = {};
    data.fn.getTeamUser = function($team,$teamUser) {
        var teamSelId = $team.val();
        var teamUser = {},manager = $global.fn.getUserData(),manager,userId = manager.userId,username = manager.username;

        if((auth==undefined || auth!='viewAllCustomer') && manager===false && userId>0) {
            $('<option value=' + userId + '>' + username + '</option>').appendTo($teamUser);
        } else {
            $('<option value="">--全部--</option>').prependTo($teamUser);
            $team.on('change', function () {
                var teamId = $(this).val();
                if (teamId != '' && teamId > 0) {
                    $teamUser.empty();
                    $('<option value="">--全部--</option>').appendTo($teamUser);
                    if (teamUser[teamId]) {
                        constructTeamUser(teamId,teamUser,$teamUser);
                    } else {
                        $http.get($setting.config.team.teamUserList, {tid: teamId}).send(function (data) {
                            if (data.code == '0') {
                                teamUser[teamId] = data.data;
                                constructTeamUser(teamId,teamUser,$teamUser);
                            }
                        });
                    }
                } else if (teamId == '0' || teamId =='') {// 公海用户  ||全部
                    $teamUser.empty();
                    $('<option value="">--全部--</option>').appendTo($teamUser);
                }
            });
            if(teamSelId>0) {// 团队已选中
                var request = $http.get($setting.config.team.teamUserList, {tid: teamSelId})
                request.async = false;
                request.send(function (data) {
                    if (data.code == '0') {
                        teamUser[teamSelId] = data.data;
                        constructTeamUser(teamSelId,teamUser,$teamUser);
                    }
                });
            }
        }
        return teamUser;
    };
    data.fn.getTeam = function($team,$teamUser) {
        var request,userData=$global.fn.getUserData(),tid = userData.tid,teamUser={};
        $('<option value="">--全部--</option>').prependTo($team);
        request = $http.get($setting.config.team.teamList, {});
        request.async = false;
        request.send(function (res) {
            if (res.code == '0') {
                auth=res.expand;
                var obj = res.data;
                for (var i in obj) {
                    if(tid === obj[i].id) {
                        $('<option value=' + obj[i].id + ' selected="true">' + obj[i].name + '</option>').appendTo($team);
                    } else {
                        $('<option value=' + obj[i].id + '>' + obj[i].name + '</option>').appendTo($team);
                    }
                }
                if(auth!=undefined && auth==='viewAllCustomer'){
                    $('<option value=0>公海用户</option>').appendTo($team);
                }
                // 加载客服列表
                teamUser =  data.fn.getTeamUser($team,$teamUser);
            }
        });
        return teamUser;
    };
    data.fn.getSelTeamUserids = function(teamUser,$team,$teamUser) {
        var memberIds = '';
        if($teamUser.val()!='') {
            memberIds = $teamUser.val()
        } else {
            if($team.val()!='') {
                memberIds = $team.val() == 0 ? 0 : getTeamUserids_(teamUser[$team.val()]);
            } else {
                memberIds = '';//所有数据
            }
        }
        return memberIds;
    };
    var constructTeamUser = function (teamId,teamUser,$teamUser) {// 私有方法
        var data = teamUser[teamId];
        if (data) {
            for (var i in data) {
                $('<option value=' + data[i].userId + '>' + data[i].userName + '</option>').appendTo($teamUser);
            }
        }
    };
    var getTeamUserids_ = function (teamUser) {
        var userids = "";
        for (var i in teamUser) {
            userids += teamUser[i].userId + ","
        }
        return userids;
    };
    data.fn.getTeamUserids = getTeamUserids_;
    exports('service', data);
});